package com.study.it.xw.controller;

import com.study.it.xw.dao.UserDao;
import com.study.it.xw.entity.User;
import com.study.it.xw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    @Autowired
    private UserService service;

    @Autowired
    private UserDao dao;
    @GetMapping("/reg")
    public String reg(){
        return "reg";
    }

    @PostMapping("/reg")
    public String reg(User u){
        u.setScore(20);
        u.setAttionCount(0);
        service.reg(u);
//        dao.insertUser(u);
        return null;
    }



//    获取登录页面
    @GetMapping("/login")
    public String login(){
        return "login";
    }

//    处理登录验证
    @PostMapping("/login")
    public String login(String uname, String upwd, HttpSession session, Model m){
        User u=service.login(uname,upwd);
        if(u==null){
            m.addAttribute("err","用户名或密码不对");
            return "login";
        }
        session.setAttribute("user",u);
        return "redirect:index";
    }

//   @GetMapping("/list")
//    public String list(){
//        return "list";
//    }

}
